<template>
  <div>
    <!-- 添加业务授权内的添加产品的 对话框(添加 / 修改) -->
    <el-dialog :title="config.title" :visible.sync="config.open" width="500px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="config.form" :rules="config.rules" label-width="140px">
        <!-- <el-form-item label="混合云客户授权管理id" prop="hbcClientAuthId">
          <el-input v-model="form.hbcClientAuthId" placeholder="请输入混合云客户授权管理id" />
        </el-form-item> -->
        <el-form-item label="产品名称" prop="productName">
          <!-- <el-input v-model="form.productName" placeholder="请输入产品名称" /> -->
          <el-select clearable v-model="config.form.productName" filterable placeholder="请选择">
            <el-option v-for="item in hbcProductNameList.filter(item=>item.productType=='hbc_product_type_func')" :key="item.productName" :label="item.productName" :value="item.productName" />
          </el-select>
        </el-form-item>
        <el-form-item label="版本号" prop="productVersion">
          <!-- <el-input v-model="form.productVersion" placeholder="请输入版本号" /> -->
          <el-select clearable v-model="config.form.productVersion" filterable placeholder="请选择">
            <el-option v-for="item in hbcProductVersionList.filter(item=>item.productType=='hbc_product_type_func')" :key="item.productVersion" :label="item.productVersion" :value="item.productVersion" />
          </el-select>
        </el-form-item>
        <el-form-item label="产品数量" prop="productQuantity">
          <el-input v-model="config.form.productQuantity" placeholder="请输入产品数量" />
        </el-form-item>
        <el-form-item label="授权有效期类型" prop="authType">
          <!-- <el-select v-model="form.authType" placeholder="请选择授权有效期类型">
            <el-option label="请选择字典生成" value="" />
          </el-select> -->
          <el-radio-group v-model="config.form.authType">
            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.HBC_PRODUCT_SCOPE_TYPE)"
                      :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="authDuration">
          <el-date-picker
            v-model="config.form.authDuration"
            style="width: 240px"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="-"
            :default-time="['00:00:00', '23:59:59']"
            @input="selectTime"
          />
          </el-form-item>
        </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-preventReClick type="primary" @click="submitForm">确 定</el-button>
        <el-button v-preventReClick @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import { getHbcProductListAll } from "@/api/hbcsupport/hbcProduct";

export default {
  props:{
    config:{
      //=== 添加业务授权，添加产品
      title:"",
      open:false,
      form:{},
      rules:{
        productName: [{ required: true, message: "产品名称不能为空", trigger: "blur" }],
        productVersion: [{ required: true, message: "版本号不能为空", trigger: "blur" }],
        productQuantity: [{ pattern: /^[1-9]\d*$/, required: true, message: "产品数量能为空", trigger: "blur" }],
        authType: [{ required: true, message: "授权有效期类型不能为空", trigger: "change" }],
      },
    },
  },
  data() {
    return {
      editId: 0,
      hbcProductNameList:[],
      hbcProductVersionList:[],
    }
  },
  mounted() {
    this.getHbcProductListAll();
  },
  methods: {
    getHbcProductListAll(){
      getHbcProductListAll({}).then(response => {
        const hbcProductNameListMap = new Map();
        const hbcProductVersionMap = new Map();
        const hbcProductNameList = [];
        const hbcProductVersionList = [];
        response.data.forEach(item => {
          if (!hbcProductNameListMap.has(item.productName)) {
            hbcProductNameListMap.set(item.productName, true)
            hbcProductNameList.push(item);
          }
          if (!hbcProductVersionMap.has(item.productVersion)) {
            hbcProductVersionMap.set(item.productVersion, true)
            hbcProductVersionList.push(item);
          }
        });
        // 上面去重
        this.hbcProductNameList = response.data;
        console.log(response.data,'授权这里',this.hbcProductNameList,'this.hbcProductNameList---')
        this.hbcProductVersionList = response.data;
      });
    },
    selectTime(data) {
      this.$nextTick(() => {
        this.$set(this.config.form, 'time', [data[0], data[1]]);
      });
    },
    reset() {
      this.resetForm("form");
    },
    submitForm(){
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        if(this.config.form.authDuration){
          this.config.form.authDurationBegin = new Date(this.config.form.authDuration[0]).getTime();
          this.config.form.authDurationEnd = new Date(this.config.form.authDuration[1]).getTime();
        }
        let record = JSON.parse(JSON.stringify(this.config.form));
        this.config.open = false;
        this.$emit("on-submit",record);
      });// end valid
    },
    cancel(){
      this.config.open = false;
    },
  }
}
</script>
